package com.zlx.sharelive.fragment;

import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.zlx.module_base.base_api.res_data.DynamicLikeListBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_fg.BaseFg;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.R;
import com.zlx.sharelive.adapter.CommonRecyclerViewAdapter;
import com.zlx.sharelive.adapter.CommonRecyclerViewHolder;
import com.zlx.sharelive.utils.GlideImgManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 评论——文章
 */
public class CommentArticleFg extends BaseFg {

    @BindView(R.id.sw_layout)
    SwipeRefreshLayout sw_layout;
    @BindView(R.id.rv_data)
    RecyclerView rv_data;

    private int page = 1;
    private String isSelf = "", isTj = "", city = "", izFee = "";
    private List<DynamicLikeListBean.InfoDTO.ResultDTO.RecordsDTO> mList = new ArrayList<>();
    private CommonRecyclerViewAdapter<DynamicLikeListBean.InfoDTO.ResultDTO.RecordsDTO> mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fg_article;
    }

    @Override
    protected void initViews() {
        super.initViews();

        initRecyclerView();
        getDynamicCommentList();

        sw_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getDynamicCommentList();
            }
        });
    }


    private void getDynamicCommentList() {
        ApiUtil.getServiceApi().getDynamicCommentList(SPUtil.getToken(getContext()),
                "0", String.valueOf(page)).observe(getViewLifecycleOwner(), new BaseObserver<>(new BaseObserverCallBack<ApiResponse<DynamicLikeListBean>>() {
            @Override
            public void onSuccess(ApiResponse<DynamicLikeListBean> data) {
                sw_layout.setRefreshing(false);
                mList.clear();
                if (null != data) {
                    if (null != data.getData()) {
                        if (null != data.getData().getInfo()) {
                            if (null != data.getData().getInfo().get(0).getResult()) {
                                mList.addAll(data.getData().getInfo().get(0).getResult().getRecords());
                            }
                        }
                    }
                }
                mAdapter.notifyDataSetChanged();
            }
        }));
    }


    /**
     * 初始化列表
     */
    private void initRecyclerView() {
        rv_data.setLayoutManager(new LinearLayoutManager(getContext()));

        mAdapter = new CommonRecyclerViewAdapter<DynamicLikeListBean.InfoDTO.ResultDTO.RecordsDTO>(getContext(), mList) {
            @Override
            public void convert(CommonRecyclerViewHolder holder, DynamicLikeListBean.InfoDTO.ResultDTO.RecordsDTO entity, int position) {
                ImageView iv_community_head = holder.getView(R.id.iv_community_head);

                GlideImgManager.glideLoader(getContext(), entity.getAvatar(), iv_community_head, 0);

                holder.setText(R.id.tv_community_nickname, entity.getNickname());

                ImageView iv_community_vip = holder.getView(R.id.iv_community_vip);
                ImageView iv_community_goddess = holder.getView(R.id.iv_community_goddess);

                holder.setText(R.id.tv_community_text_title, entity.getTitle());
                holder.setText(R.id.tv_community_text_content, entity.getText());

                holder.setText(R.id.tv_community_address, entity.getComment());
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_community_type1_2;
            }
        };

        rv_data.setAdapter(mAdapter);

        mAdapter.setOnRecyclerViewItemClickListener(new CommonRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
//                startActivity(new Intent(getContext(), CommunityDetailsAc.class)
//                        .putExtra("id", mList.get(position).getId()));
            }
        });
    }
}
